import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 用本地缓存需要注意：用户有可能关闭浏览器缓存，一旦获取不到就会程序崩溃
let defaultCity = "深圳"
try{
	if(localStorage.city){
		defaultCity = localStorage.city
	}
}catch (e) {}

export default new Vuex.Store({
	state: {
		// 默认城市
		city: defaultCity
	},
	actions: {
		changeCityAction (ctx,city) {
			// 靠ctx触发commit
			ctx.commit('changeCityMutation',city)
		}
	},
	mutations: {
		changeCityMutation (state,city) {
			// state.city是公共数据
			state.city = city

			// 设置缓存中的城市
			try{
				localStorage.city = city
			}catch (e) {}
		}
	}
})